<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <!-- 搜索区域 -->
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
            @change="searc_user"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="searc_user"
            />
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="add_user">添加用戶</el-button>
        </el-col>
      </el-row>
      <el-table :data="usrList" style="width: 100%">
        <el-table-column type="index" label="#" />
        <el-table-column
          v-for="(item, index) in tabList"
          :key="index"
          :prop="item.prop"
          :label="item.label"
        />
        <el-table-column label="状态">
          <template slot-scope="{ row }">
            <el-switch
              v-model="row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="switch_change(row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="edit_user(row.id)"
            />
            <el-button
              size="mini"
              icon="el-icon-delete"
              type="danger"
              @click="delete_user(row.id)"
            />
            <el-button size="mini" icon="el-icon-star-off" type="warning" />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.pagenum"
        :page-sizes="[2, 4, 6, 8, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </el-card>
    <dialogBox :dialogVisible.sync="dialogVisible" :node_id="node_id" />
  </div>
</template>

<script>
import dialogBox from '@/components/user/component/dialogBox.vue'
export default {
  name: 'VueShopUser',
  components: {
    dialogBox,
  },
  data() {
    return {
      dialogVisible: false,
      node_id: '',
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2,
      },
      usrList: [],
      total: null,
      tabList: [
        {
          prop: 'username',
          label: '姓名',
          width: '200',
        },
        {
          prop: 'email',
          label: '邮箱',
          width: '200',
        },
        {
          prop: 'mobile',
          label: '电话',
          width: '200',
        },
        {
          prop: 'role_name',
          label: '角色',
          width: '200',
        },
      ],
    }
  },
  created() {
    this.getUserLits()
  },
  mounted() {},

  methods: {
    async getUserLits() {
      await this.$axios.getUserList(this.queryInfo).then((res) => {
        if (res.meta.status !== 200) {
          return this.$message.error('获取用户列表失败')
        }
        const { data } = res
        this.total = data?.total || null
        this.usrList = data?.users || []
      })
    },
    handleSizeChange(val) {
      //当前页几条数据
      this.queryInfo.pagesize = val
      this.getUserLits()
    },
    handleCurrentChange(val) {
      //第几页
      this.queryInfo.pagenum = val
      this.getUserLits()
    },
    //用户状态
    async switch_change(val) {
      console.log(val)
      await this.$axios.editUserState(val.id, val.mg_state).then((res) => {
        const { data, meta } = res
        if (meta?.status !== 200) return this.$message.error(meta?.msg)
        this.$message.success(meta?.msg)
        this.getUserLits()
      })
    },
    //添加用户
    add_user() {
      this.node_id = ''
      this.dialogVisible = true
    },
    //编辑用户
    async edit_user(id) {
      this.node_id = id
      // await this.$axios.edit_user(id)
      this.dialogVisible = true
    },
    //删除用户
    async delete_user(id) {
      await this.$axios.delete_user(id).then((res) => {
        const { meta } = res
        if (meta?.status !== 200) return this.$message.error(meta?.msg)
        this.$message.success(meta?.msg)
        this.getUserLits()
      })
    },
    //搜索用户
    searc_user() {
      this.getUserLits()
    },
  },
}
</script>

<style lang="scss" scoped>
</style>
